package org.itboys.mobile.controller.mobile.common;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.service.mongo.mobile.common.SundayMobileCidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：jiangxiong
 * 日期：2017年2月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_移动端（通用）_cid_controller。
 */
@RestController
@RequestMapping("/sunday/mobile/cid")
@Api(value = "cid",description = "移动端cid")
public class SundayMobileCidController extends BaseController {

    @Autowired
    private SundayMobileCidService cidService;



    /**
     * 上传手机CID
     * @param request
     * @param response
     * @param memberId
     * @param type
     * @param cid
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ApiOperation(value = "上传手机CID。登陆后调用")
    public void upload(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "memberId", value = "用户Id。") @RequestParam(value = "memberId", required = true) Long memberId,
            @ApiParam(required = true, name = "type", value = "类型。IOS/ANDROID(固定值)。") @RequestParam(value = "type", required = true) String type,
            @ApiParam(required = true, name = "cid", value = "手机cid。") @RequestParam(value = "cid", required = true) String cid,
            @ApiParam(required = true, name = "timestamp", value = "当前系统时间戳") @RequestParam(value = "timestamp", required = true) long timestamp,
            @ApiParam(required = true, name = "token", value = "签名") @RequestParam(value = "token", required = true) String token){

        try {
            cidService.upload(memberId,type,token);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgResultDO( CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.info("/sunday/mobile/cid/upload--------"+e);
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));

        }
    }


}
